using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Text.RegularExpressions;

namespace SpiderService
{
	public class ConfigDatas
	{
		private static Dictionary<int, SpiderProject> s_Projects;
		private static object s_ObjLock;
		private static bool s_inited;

		static ConfigDatas()
		{
			s_ObjLock = new object();
			s_inited = false;
		}

		public static void ReInit()
		{
			s_inited = false;
			Init();
		}

		public static void Init()
		{
			if (s_inited)
				return;
			lock (s_ObjLock)
			{
				com.spider.SpiderService spider = Globals.SpiderWebService;
				DataTable dtProjects = spider.GetSpiderProjects();
				DataTable dtFileConfigs = spider.GetSpiderFileConfigs();
				DataTable dtFileFieldConfigs = spider.GetSpiderFileFieldConfigs();

				if (dtProjects.Rows.Count > 0)
				{
					s_Projects = new Dictionary<int, SpiderProject>(dtProjects.Rows.Count);
					foreach (DataRow drPro in dtProjects.Rows)
					{
						SpiderProject project = new SpiderProject();
						project.ProjectId = Convert.ToInt32(drPro["ProjectId"]);
						project.StartUrl = Convert.ToString(drPro["StartUrl"]);
						project.Cookies = Convert.ToString(drPro["Cookies"]);
						project.Encoding = Convert.ToString(drPro["Encoding"]);
						project.AcceptLanguage = Convert.ToString(drPro["AcceptLanguage"]);
						project.FileRules = new List<FileRule>();

						DataRow[] drFiles = dtFileConfigs.Select("ProjectId='" + project.ProjectId + "'");
						if (drFiles != null)
						{
							foreach (DataRow drFile in drFiles)
							{
								FileRule fileRule = new FileRule();
								fileRule.FileId = Convert.ToInt32(drFile["FileId"]);
								fileRule.Name = Convert.ToString(drFile["Name"]);
								fileRule.Patterns = Convert.ToString(drFile["Patterns"]);
								fileRule.FullTypeName = Convert.ToString(drFile["FullTypeName"]);
								fileRule.HandleMethodName = Convert.ToString(drFile["HandleMethodName"]);
								fileRule.ImportDataMethodName = Convert.ToString(drFile["ImportDataMethodName"]);
								fileRule.ExtractLink = Convert.ToBoolean(Convert.ToByte(drFile["ExtractLink"]));
								fileRule.FieldRules = new List<FieldRule>();

								DataRow[] drFields = dtFileFieldConfigs.Select("FileId='" + fileRule.FileId + "'");
								if (drFields != null)
								{
									foreach (DataRow drField in drFields)
									{
										FieldRule fieldRule = new FieldRule();
										fieldRule.FieldId = Convert.ToInt32(drField["FieldId"]);
										fieldRule.Name = Convert.ToString(drField["Name"]);
										fieldRule.Code = Convert.ToString(drField["Code"]);
										fieldRule.Patterns = Convert.ToString(drField["Patterns"]);
										fieldRule.IsDataTitle = Convert.ToBoolean(Convert.ToByte(drField["IsDataTitle"]));

										fileRule.FieldRules.Add(fieldRule);
									}
								}

								project.FileRules.Add(fileRule);
							}
						}

						s_Projects.Add(project.ProjectId, project);
					}
				}
			}
			s_inited = true;
		}

		public static SpiderProject GeProject(int projectId)
		{
			if (s_Projects == null)
				return null;

			lock (s_ObjLock)
			{
				SpiderProject project;
				s_Projects.TryGetValue(projectId, out project);
				return project;
			}
		}

		public static bool IsMathFile(int projectId, string url, out FileRule fileRule)
		{
			fileRule = null;
			try
			{
				SpiderProject project = ConfigDatas.GeProject(projectId);
				if (project != null)
				{
					foreach (FileRule fr in project.FileRules)
					{
						Regex regex = new Regex(string.Format("^{0}$", fr.Patterns));
						if (regex.IsMatch(url))
						{
							fileRule = fr;
							return true;
						}
					}
				}
			}
			catch (Exception ex)
			{
				Globals.FileLog.WriteError(ex);
			}
			return false;
		}

		public static FileRule GetFile(string url)
		{
			if (s_Projects == null)
			{
				return null;
			}

			FileRule fileRule = null;
			foreach (KeyValuePair<int, SpiderProject> kv in s_Projects)
			{

			}

			return null;
		}

	}

	public class SpiderProject
	{
		public int ProjectId;
		public string Encoding;
		public string Cookies;
		public string AcceptLanguage;
		public string StartUrl;
		public List<FileRule> FileRules;

		public FileRule FindFileRule(int fileId)
		{
			foreach (FileRule fr in this.FileRules)
			{
				if (fr.FileId == fileId)
					return fr;
			}
			return null;
		}
	}

	public class FileRule
	{
		public int FileId;
		public string Name;
		public string Patterns;
		public string FullTypeName;
		public string HandleMethodName;
		public string ImportDataMethodName;
		public bool ExtractLink;
		public List<FieldRule> FieldRules;
	}

	public class FieldRule
	{
		public int FieldId;
		public string Name;
		public string Code;
		public string Patterns;
		public bool IsDataTitle;
	}

	[Serializable()]
	public class WebFileInfo
	{
		public int ProjectId;
		public string Url;
	}

	[Serializable()]
	public class WebDataInfo
	{
		public int ProjectId;
		public string Url;
		public string Data;
	}

}
